import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_study/utils/toast.dart';
import 'dart:io';

class GoAndroidActivity extends StatefulWidget {
  String title;

  GoAndroidActivity({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return AndroidActivityState();
  }
}

class AndroidActivityState extends State<GoAndroidActivity> {
  //步骤一 请确保cn.mrlong.study/plugin 和 MainActivity中一直
  static const PLATFORM_ANDROID = const MethodChannel('cn.mrlong.study/plugin');
  static const PLATFORM_IOS = const MethodChannel('cn.mrlong.study.ios/plugin');

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(this.widget.title),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            goActivity();
          },
          child: Text('跳转到Activity'),
        ),
      ),
    );
  }

  void goActivity() async {
    //步骤二
    //其实这里可以不用判断：只是为了启动不同的原生界面
    if (Platform.isAndroid) {
      String res = await PLATFORM_ANDROID.invokeMethod('data', 'playerUrl');
      Toast.toast(context, res);
    }else if(Platform.isIOS){
      String res = await PLATFORM_IOS.invokeMethod('data', 'playerUrlIos');
      Toast.toast(context, res);
    }else{
      Toast.toast(context, '暂时不支持这个平台');
    }
  }
}
